﻿using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.Presenter.Support
{
    public abstract class BaseControl : UserControl
    {
        //
        public void HandleError(Exception e)
        {
            string str = e.Message;
            string showtrace = ConfigurationHelper.GetInstance().ConfigItems[GlobalConstants.TRAZA_ERRORES];
            if (!string.IsNullOrEmpty(showtrace))
                if(showtrace.ToUpper() == "True".ToUpper())
                    str = "Mensaje Usuario : " + e.Message + "--------------Source : " + e.Source + "-----------StackTrace  : " + e.StackTrace;

            StringBuilder javascript = new StringBuilder(@"<script type='text/javascript'>");
            javascript.Append("ExecuteOrDelayUntilScriptLoaded(function(){SP.UI.Status.removeStatus(statusID); statusID=null;");
            javascript.Append("var statusID = '';");
            javascript.Append(string.Format("statusID=SP.UI.Status.addStatus('', '{0}');", str.Replace("\r", " ").Replace("\n", "<br/>").Replace("'", "&quot;")));
            javascript.Append("SP.UI.Status.setStatusPriColor(statusID, 'red');");            
            javascript.Append("$('#s4-workspace').animate({ scrollTop: 0 });");
            //javascript.Append("setTimeout(function(){SP.UI.Status.removeStatus(statusID); statusID=null;}, 6000);}, 'sp.js');");
            javascript.Append(";}, 'sp.js');");
            javascript.Append("</script>");
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "errorscript", javascript.ToString());
            Logger.LogException(e, Microsoft.SharePoint.Administration.EventSeverity.Error,
                SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
        }

        public void HandleSuccess(string message, WebControl[] disableControls)
        {
            HandleSuccess(message, disableControls, false);
        }

        public void HandleSuccess(string message, WebControl[] disableControls, bool closeModal)
        {
            StringBuilder javascript = new StringBuilder(@"<script type='text/javascript'>");
            javascript.Append("ExecuteOrDelayUntilScriptLoaded(function(){");
            javascript.Append("var statusID = '';");
            javascript.Append(string.Format("statusID=SP.UI.Status.addStatus('', '{0}');", message));
            javascript.Append("SP.UI.Status.setStatusPriColor(statusID, 'green');");            
            javascript.Append("$('#s4-workspace').animate({ scrollTop: 0 });");
            //javascript.Append("setTimeout(function(){SP.UI.Status.removeStatus(statusID); statusID=null;}, 6000);}, 'sp.js');");
            javascript.Append(";}, 'sp.js');");
            javascript.Append("</script>");
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "successscript", javascript.ToString());
            //Logger.LogMessage(message, SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
            if (disableControls == null) return;
            foreach (var control in disableControls)
            {
                control.Enabled = false;
            }
        }

        public void HandleWarning(string message)
        {
            StringBuilder javascript = new StringBuilder(@"<script type='text/javascript'>");
            javascript.Append("ExecuteOrDelayUntilScriptLoaded(function(){SP.UI.Status.removeStatus(statusID); statusID=null;");
            javascript.Append("var statusID = '';");
            javascript.Append(string.Format("statusID=SP.UI.Status.addStatus('', '{0}');", message));
            javascript.Append("SP.UI.Status.setStatusPriColor(statusID, 'yellow');");            
            javascript.Append("$('#s4-workspace').animate({ scrollTop: 0 });");
            //javascript.Append("setTimeout(function(){SP.UI.Status.removeStatus(statusID); statusID=null;}, 6000);
            javascript.Append("}, 'sp.js');");
            javascript.Append("</script>");
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "warningscript", javascript.ToString());
            //Logger.LogMessage(message, SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
        }

    }
}
